import csv
import random
from datetime import datetime
from time import strftime
import numpy as np
from scipy.stats import mannwhitneyu
from scipy.stats import scoreatpercentile


# Mann Whitney U test


def getCurTime():
    """
    get current time
    Return value of the date string format(%Y-%m-%d %H:%M:%S)
    """
    format='%Y-%m-%d %H:%M:%S'
    sdate = None
    cdate = datetime.now()
    try:
        sdate = cdate.strftime(format)
    except:
        raise ValueError
    return sdate

def build_data_list(inputCSV):
    sKey = []
    fn = inputCSV
    ra = csv.DictReader(file(fn), dialect="excel")
    
    for record in ra:
        #print record[ra.fieldnames[0]], type(record[ra.fieldnames[-1]])
        for item in ra.fieldnames:
            temp = float(record[item])
            sKey.append(temp)
    sKey = np.array(sKey)
    sKey.shape=(-1,len(ra.fieldnames))
    return sKey



#--------------------------------------------------------------------------
#MAIN
if __name__ == "__main__":
    print "================"
    print "begin at " + getCurTime()
    inputCSV = 'C:/_DATA/CancerData/test/Jun08/total.csv'
    data = build_data_list(inputCSV)
    print 't-statistic = %6.3f pvalue = %6.4f' %  stats.ttest_1samp(data[:,0]-data[:,1], 0)
    pvalue = np.zeros((len(data[0]),len(data[0])))
    for i in range(0, len(data[0])):
        for j in range(i+1, len(data[0])):
            a, b = stats.ttest_1samp(data[:,i] - data[:,j], 0)
            pvalue[i,j] = b
            pvalue[j,i] = b
    print pvalue
    np.savetxt('C:/temp2.csv', pvalue, delimiter=',', fmt = '%10.5f')
    '''
    for i in range(0, len(data[0])):
        print np.mean(data[:,i]), scoreatpercentile(data[:,i], 25), scoreatpercentile(data[:,i], 50), scoreatpercentile(data[:,i], 75)
    
    #cal mww test
    pvalue = np.zeros((len(data[0]),len(data[0])))
    for i in range(0, len(data[0])):
        for j in range(i+1, len(data[0])):
            a, b = mannwhitneyu(data[:,i], data[:,j])
            pvalue[i,j] = b
            pvalue[j,i] = b
    #print pvalue
    #np.savetxt('C:/temp2.csv', pvalue, delimiter=',', fmt = '%10.5f')
    '''

